package json;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.util.List;
import java.util.Map;

public class Json_demo {
    public static void main(String[] args) {
        Gson gson = new Gson();
        JsonService service = new JsonService();

        String str1 = gson.toJson(service.getPersion());
        System.out.println("1:"+str1);
//      1:{"id":"123","name":"sisi","place":"BeiJing"},1:{"id":"2002","name":"bobo","place":"HanZhou"},

        String str4 = gson.toJson(service.getMap());
        System.out.println("2: "+str4);
//       2: {"id":"123"."name:": "sisi","place":"HangZhou"}

        String str2 = gson.toJson(service.getPersonList());
        System.out.println("3: "+ str2);
//       3: {"id" : 123,"name" : "sisi","place" : "BeiJing"}
//           {"id" :2002,"name" : "bobo" , "place" : "HangZhou"}


        String str3 = gson.toJson(service.getList());
        System.out.println("4: "+str3);
//      4 : {"BeiJing","HangZhou"}

         String str5 = gson.toJson(service.getlistMap());
         System.out.println("5: "+ str5);

         Person personA = gson.fromJson(str4, Person.class);
         System.out.println(personA);

        Map<String,Object> mapA = gson.fromJson(str4,Map.class);
        System.out.println(mapA);

        List<Map> list = gson.fromJson(str5 /*str2*/, List.class);
            for (Map map:list){
                String s = gson.toJson(map);
                Person p = gson.fromJson(s, Person.class);
                System.out.println(p);
            }

            List<Map<String,Object>> list_map = gson.fromJson(str5, new TypeToken<List<Map<String,Object>>>() {}.getType());
            for (Map m : list_map){
                System.out.println(m);
            }


            List<Person> personList = gson.fromJson(str2 /*str5*/,new TypeToken<List<Person>>() {}.getType());
            for (Person p : personList) {
                System.out.println(p);
            }
//                    json.Person [id=123, name=sisi, place=BeiJing]
//                      Perso n [id=2002, name=bobo, place=HangZhou]
    }
}